# -*-coding=UTF-8-*-
from django.http import JsonResponse
from utils.backcode import ImportError

from utils.response_utils import response_change

role_black_list = [[], [],
                   ['/userManage/creatUser', '/userManage/resetPassword', '/userManage/userList',
                    '/userManage/updatePermit',
                    '/userManage/permitList', '/userManage/userPermit', '/userManage/changePasswd',
                    '/userManage/showChecked',
                    ],
                   ['/userManage/creatUser', '/userManage/resetPassword', '/userManage/userList',
                    '/userManage/updatePermit',
                    '/userManage/permitList', '/userManage/userPermit', '/userManage/changePasswd',
                    '/userManage/showChecked',
                    '/userManage/userListPermit', '/userManage/allUser',
                    ]]

post_black_list = [
    ['/task/taskAdd', '/task/taskDel', '/task/taskEdit', '/task/taskMemberAdd', '/task/taskMemberDel',
     '/task/taskMemberEdit', '/task/taskMemberUserAdd', '/task/taskMemberUserEdit', '/task/taskEditLeader',
     '/project/projectAdd', '/project/projectDel', '/project/projectEdit', '/project/projectGroupAdd',
     '/project/projectOver', '/task/taskOver',
     '/project/projectGroupDel', '/project/projectGroupEdit', '/project/projectGroupAddUser',
     '/project/projectGroupEditUser', '/project/projectGroupDelUser', '/project/projectEditLeader'],
    ['/task/taskAdd', '/task/taskDel', '/task/taskEdit', '/task/taskMemberAdd', '/task/taskMemberDel',
     '/task/taskMemberEdit', '/task/taskMemberUserAdd', '/task/taskMemberUserEdit', '/task/taskEditLeader',
     '/project/projectAdd', '/project/projectDel', '/project/projectEdit', '/project/projectGroupAdd',
     '/project/projectGroupDel', '/project/projectGroupEdit', '/project/projectGroupAddUser', '/project/projectOver',
     '/task/taskOver',
     '/project/projectGroupEditUser', '/project/projectGroupDelUser', '/project/projectEditLeader'],
    ['/project/projectAdd', '/project/projectDel', '/project/projectEdit', '/project/projectGroupAdd',
     '/project/projectGroupDel', '/project/projectGroupEdit', '/project/projectGroupAddUser', '/project/projectOver',
     '/project/projectGroupEditUser', '/project/projectGroupDelUser', '/project/projectEditLeader'],
    []]


def power_middleware(get_response):
    def middleware(request):
        company_post = request.session.get("company_post")
        role = request.session.get("role")
        if request.path != "/userManage/userLogin" and request.path != "/userManage/initUser":
            if company_post == None or role == None:
                data = {"response_msg": "请登录操作,亲", "response_code": ImportError.POWER_NOT_ALLOW}
                response = JsonResponse(data)
                return response_change(response)
        if company_post <= 3 and company_post >= 0:
            if request.path in post_black_list[company_post]:
                data = {"response_msg": "权限不足", "response_code": ImportError.POWER_NOT_ALLOW}
                response = JsonResponse(data)
                return response_change(response)
        elif company_post > 5:
            data = {"response_msg": "权限不足", "response_code": ImportError.POWER_NOT_ALLOW}
            response = JsonResponse(data)
            return response_change(response)
        if role < 3 and role > 0:
            if request.path in role_black_list[role]:
                data = {"response_msg": "权限不足", "response_code": ImportError.POWER_NOT_ALLOW}
                response = JsonResponse(data)
                return response_change(response)
        response = get_response(request)
        return response
    
    return middleware
